package com.supermarket.business.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.supermarket.business.domain.SupermarketAdmin;
import com.supermarket.business.domain.SupermarketCashier;
import com.supermarket.business.domain.SupermarketWarehouseManager;
import com.supermarket.business.mapper.SupermarketAdminMapper;
import com.supermarket.business.mapper.SupermarketCashierMapper;
import com.supermarket.business.vo.SupermarketWarehouseManagerVO;
import com.supermarket.common.core.domain.entity.SysUser;
import com.supermarket.common.utils.DateUtils;
import com.supermarket.common.utils.SecurityUtils;
import com.supermarket.system.mapper.SysUserMapper;
import com.supermarket.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.supermarket.business.mapper.SupermarketWarehouseManagerMapper;
import com.supermarket.business.service.ISupermarketWarehouseManagerService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 仓库管理员Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-21
 */
@Service
public class SupermarketWarehouseManagerServiceImpl extends ServiceImpl<SupermarketWarehouseManagerMapper, SupermarketWarehouseManager> implements ISupermarketWarehouseManagerService
{
    @Resource
    private SupermarketWarehouseManagerMapper supermarketWarehouseManagerMapper;
    @Resource
    private SupermarketAdminMapper supermarketAdminMapper;
    @Resource
    private SupermarketCashierMapper supermarketCashierMapper;
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 查询仓库管理员
     * 
     * @param managerId 仓库管理员主键
     * @return 仓库管理员
     */
    @Override
    public SupermarketWarehouseManager selectSysWarehouseManagerByManagerId(Long managerId)
    {
        return supermarketWarehouseManagerMapper.selectSysWarehouseManagerByManagerId(managerId);
    }

    /**
     * 查询仓库管理员列表
     * 
     * @param supermarketWarehouseManager 仓库管理员
     * @return 仓库管理员
     */
    @Override
    public List<SupermarketWarehouseManager> selectSysWarehouseManagerList(SupermarketWarehouseManager supermarketWarehouseManager)
    {
        return supermarketWarehouseManagerMapper.selectSysWarehouseManagerList(supermarketWarehouseManager);
    }

    /**
     * 新增仓库管理员
     * 
     * @param supermarketWarehouseManager 仓库管理员
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysWarehouseManager(SupermarketWarehouseManager supermarketWarehouseManager)
    {
        //校验账号id是否合理
        if(Objects.nonNull(supermarketWarehouseManager.getSysId())){
            SysUser user = sysUserMapper.getUserById(supermarketWarehouseManager.getSysId());
            if(Objects.nonNull(user.getBeUsed())&&!Objects.equals(user.getBeUsed(),supermarketWarehouseManager.getManagerId())){
                throw new IllegalArgumentException("该账号已被使用，请更换账号！");
            }
            sysUserMapper.updateBeUsed(supermarketWarehouseManager.getManagerId(), user.getUserId());
        }
        return supermarketWarehouseManagerMapper.insertSysWarehouseManager(supermarketWarehouseManager);
    }

    /**
     * 修改仓库管理员
     * 
     * @param supermarketWarehouseManager 仓库管理员
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysWarehouseManager(SupermarketWarehouseManager supermarketWarehouseManager)
    {
        //将之前的账号id解锁
        SupermarketWarehouseManager supermarketWarehouseManager1 = supermarketWarehouseManagerMapper.selectSysWarehouseManagerByManagerId(supermarketWarehouseManager.getManagerId());
        SysUser user1 = sysUserMapper.getUserById(supermarketWarehouseManager1.getSysId());
        if(Objects.nonNull(user1)){
            sysUserMapper.updateBeUsed(null, user1.getUserId());
        }

        //校验账号id是否合理
        if(Objects.nonNull(supermarketWarehouseManager.getSysId())){
            SysUser user = sysUserMapper.getUserById(supermarketWarehouseManager.getSysId());
            if(Objects.nonNull(user.getBeUsed())&&!Objects.equals(user.getBeUsed(),supermarketWarehouseManager.getManagerId())){
                throw new IllegalArgumentException("该账号已被使用，请更换账号！");
            }
            sysUserMapper.updateBeUsed(supermarketWarehouseManager.getManagerId(), user.getUserId());
        }
        return supermarketWarehouseManagerMapper.updateSysWarehouseManager(supermarketWarehouseManager);
    }

    /**
     * 批量删除仓库管理员
     * 
     * @param managerIds 需要删除的仓库管理员主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysWarehouseManagerByManagerIds(Long[] managerIds)
    {
        for (Long managerId : managerIds) {
            updateSysId(managerId);
        }
        return supermarketWarehouseManagerMapper.deleteSysWarehouseManagerByManagerIds(managerIds);
    }

    /**
     * 删除仓库管理员信息
     * 
     * @param managerId 仓库管理员主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysWarehouseManagerByManagerId(Long managerId)
    {
        updateSysId(managerId);
        return supermarketWarehouseManagerMapper.deleteSysWarehouseManagerByManagerId(managerId);
    }

    /**
     * 查询仓库管理员VO列表
     * @param supermarketWarehouseManager
     * @return
     */
    @Override
    public List<SupermarketWarehouseManagerVO> selectSysWarehouseManagerListVO(SupermarketWarehouseManager supermarketWarehouseManager) {
        List<SupermarketWarehouseManager> list = supermarketWarehouseManagerMapper.selectSysWarehouseManagerList(supermarketWarehouseManager);
        List<SupermarketWarehouseManagerVO> listVO = new ArrayList<>();
        for (SupermarketWarehouseManager warehouseManager : list) {
            SupermarketWarehouseManagerVO warehouseManagerVO = new SupermarketWarehouseManagerVO();
            BeanUtils.copyProperties(warehouseManager, warehouseManagerVO);
            listVO.add(warehouseManagerVO);
        }
        return listVO;
    }

    /**
     * 获取仓库管理员VO
     * @param managerId
     * @return
     */
    @Override
    public SupermarketWarehouseManagerVO selectSysWarehouseManagerVOByManagerId(Long managerId) {
        SupermarketWarehouseManager warehouseManager = supermarketWarehouseManagerMapper.selectSysWarehouseManagerByManagerId(managerId);
        if(warehouseManager == null){
            throw new IllegalArgumentException("仓库管理员不存在");
        }
        SupermarketWarehouseManagerVO warehouseManagerVO = new SupermarketWarehouseManagerVO();
        BeanUtils.copyProperties(warehouseManager, warehouseManagerVO);
        return warehouseManagerVO;
    }

    @Override
    public String getNowSysId() {
        return SecurityUtils.getLoginUser().getUserId().toString();
    }

    /**
     * 释放账号id
     */
    private void updateSysId(Long warehouseManagerId){
        SupermarketWarehouseManager supermarketWarehouseManager = supermarketWarehouseManagerMapper.selectSysWarehouseManagerByManagerId(warehouseManagerId);
        if(Objects.nonNull(supermarketWarehouseManager.getSysId())){
            sysUserMapper.updateBeUsed(null, supermarketWarehouseManager.getSysId());
        }
    }
}
